/*created by hp
*on 18-10-13 下午3:03
*/
package com.happts.UI;
import com.happts.Entity.RECORD;
import com.happts.Service.RECORDService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class RecordUI {
    String[] columnNames={"姓名","地址","代收点","单号","重量","菜籽油调率","剩余菜籽油", "精制油调率","剩余精制油"};
    RECORDService rs = new RECORDService();
    List<RECORD> list = rs.findAll();
    JTable table;
    DefaultTableModel defaultTableModel;
    JLabel label;
    JTextField numberText, citeText;
    JPopupMenu popupMenu;
    int focusedRowIndex;
    Vector dataV;
    JFrame frame;
    RecordUI(){
        frame=UtilView.createFrame("记录");
        Box bottomBox=Box.createVerticalBox();
        Box topBox=Box.createHorizontalBox();
        label = new JLabel("单号+代收点查询");
        numberText =new JTextField();
        citeText =new JTextField();
        JButton find = new JButton("查询");
        JButton returnMenu = new JButton("返回菜单");

        find.addActionListener(e -> {
            String number= numberText.getText();
            String cite= citeText.getText();
            if(number.isEmpty()||cite.isEmpty()) list=rs.findAll();
            else list=rs.findOne(cite, number);
            refreshTableModel(defaultTableModel);
        });

        returnMenu.addActionListener(e -> {
            frame.dispose();
            new Menu();
        });
        topBox.add(label);topBox.add(numberText);topBox.add(citeText);
        topBox.add(find);topBox.add(returnMenu);
        bottomBox.add(initTable());
        bottomBox.add(topBox);

        createPopupMenu();
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tableMouseClicked(e);
            }
        });
        frame.setContentPane(bottomBox);
        frame.setSize(1000, 300);
//        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    public static void main(String[] args){
        new RecordUI();

    }

    public void refreshTableModel(DefaultTableModel defaultTableModel){
        Vector titleV=new Vector();
        dataV=new Vector();

        for(int i=0;i<columnNames.length;i++)
            titleV.add(columnNames[i]);

        for(RECORD record:list){
            Vector row=new Vector();
            row.add(record.getName());
            row.add(record.getAddress());
            row.add(record.getCite());
            row.add(record.getNumber());
            row.add(record.getWeight());
            row.add(record.getVegRate());
            row.add(record.getLeftVeg());
            row.add(record.getRefinedRate());
            row.add(record.getLeftRefined());
            dataV.add(row);
        }
        defaultTableModel.setDataVector(dataV,titleV);
        table.updateUI();
    }

    public JScrollPane initTable(){
        table= new JTable(initTableModel());

        return new JScrollPane(table);
    }
    public DefaultTableModel initTableModel(){
        Vector titleV = new Vector();
        dataV = new Vector();

        for (int i=0;i<columnNames.length;i++){
            titleV.add(columnNames[i]);
        }
        for (RECORD record: list){
            Vector row = new Vector();
            row.add(record.getName());
            row.add(record.getAddress());
            row.add(record.getCite());
            row.add(record.getNumber());
            row.add(record.getWeight());
            row.add(record.getVegRate());
            row.add(record.getLeftVeg());
            row.add(record.getRefinedRate());
            row.add(record.getLeftRefined());
            dataV.add(row);
        }
        defaultTableModel = new DefaultTableModel(dataV, titleV);
        return defaultTableModel;
    }


    public void tableMouseClicked(MouseEvent e){
        mouseRightButtonClick(e);
    }

    public void createPopupMenu(){
        popupMenu = new JPopupMenu();
        JMenuItem editItem = new JMenuItem();
        editItem.setText("取油");
        editItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fetchOil(e);
            }
        });
        popupMenu.add(editItem);
    }

    public void fetchOil(ActionEvent e){
        if(e.getActionCommand().equals("取油")){
            RECORD r = list.get(focusedRowIndex);
            new FetchOil(r);
            frame.dispose();
        }
    }
    public void mouseRightButtonClick(MouseEvent e){
        if (e.getButton() == MouseEvent.BUTTON3){
            focusedRowIndex = table.rowAtPoint(e.getPoint());
//            System.out.println(focusedRowIndex);
            if (focusedRowIndex == -1){
                return;
            }
            table.setRowSelectionInterval(focusedRowIndex, focusedRowIndex);
            popupMenu.show(table, e.getX(), e.getY());
        }
    }
}








